<template>
  <div class="videoProAdd">
    <el-form
      :inline="true"
      :model="ruleform"
      ref="from"
      :rules="rules"
      class="demo-form-inline ip-form"
    >
      <el-form-item label="计划名称" prop="desc">
        <el-input v-model="ruleform.desc" message placeholder></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="onSubmit('from')"
          >创建</el-button
        >
        <el-button type="warning" plain @click="onReset('from')"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
// import { Message } from "element-ui";
import { resetForm } from "@/utils/Tool";
export default {
  name: "videoProAdd",
  components: {},
  props: {},
  data() {
    var checkIsChEnNum = (rule, str, callback) => {
      //如果值为空，通过校验
      if (str == "") callback();
      var pattern = (pattern = /^[A-Za-z0-9\u4e00-\u9fa5]+$/gi);
      if (!pattern.test(str)) {
        callback(new Error("请不要输入特殊字符！"));
        return false;
      } else if (str.length > 20) {
        callback(new Error("请输入小于20个的字符！"));
        return false;
      } else {
        callback();
      }
    };
    return {
      ruleform: {
        desc: "",
      },
      rules: {
        desc: [
          {
            required: true,
            message: "请输入计划名称",
            trigger: "blur",
          },
          { validator: checkIsChEnNum, trigger: "blur" },
        ],
      },
      //}
    };
  },
  computed: {},
  created() {},
  mounted() {},
  methods: {
    //添加 提交
    onSubmit(formName) {
      var that = this;
      //   if(that.ruleform.desc.lenght<20){
      //        that.$message.error("名称");
      //   }
      that.$refs[formName].validate((valid) => {
        if (valid) {
          postData("/adminweb/mem/schedule/insert", {
            desc: that.ruleform.desc,
          })
            .then((res) => {
              console.log(res);
              that.$message.success("创建成功！");
              
              that.$store.commit(
                "setSubmitData",
                Math.floor(Math.random() * 10000000)
              );
                let datas = resetForm(that.ruleform, this.$refs[formName]);
                that.ruleform = datas;
            })
            .catch((err) => {
              that.$message.error("创建失败");
            });
        } else {
             that.$message.error("创建失败");
          return false;
        }
      });
    },
    // 重置
    onReset(val) {
      //this.$emit("reset", true);
      this.$refs[val].resetFields();
    },
  },
  watch: {},
};
</script>
<style scoped>
.videoProAdd {
  padding: 15px;
}
</style>
